package com.car.loan.project.xr.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanAlbum;
import com.car.loan.project.xr.domain.LoanDetail;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.LoanAlbumMapper;
import com.car.loan.project.xr.mapper.LoanDetailMapper;
import com.car.loan.project.xr.mapper.LoanStatusMapper;
import com.car.loan.project.xr.util.bo.business.LoanDataAuditBo;
import com.car.loan.project.xr.util.enums.DataAuditStatus;
import com.car.loan.project.xr.util.enums.LoanAlbumType;
import com.car.loan.project.xr.util.enums.PaymentSettleAccountsStatus;
import com.car.loan.project.xr.util.vo.LoanAlbumVo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author huangding
 * @description
 * @date 2019/12/18 16:07
 */
@Service
public class LoanDataAuditService {

    @Autowired
    private LoanStatusMapper loanStatusMapper;

    @Autowired
    private LoanDetailMapper loanDetailMapper;

    @Autowired
    private LoanAlbumMapper loanAlbumMapper;

    @Transactional(rollbackFor = Exception.class)
    public synchronized AjaxResult audit(LoanDataAuditBo loanDataAudit) {

        long loanId = Long.parseLong(loanDataAudit.getLoanId());
        Optional<LoanStatus> optionalLoanStatus = loanStatusMapper
            .selectByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        if (DataAuditStatus.INIT.status != loanStatus.getDataAuditStatus()) {
            return AjaxResult.error("当前状态不可提交");
        }
        //如结算信息已提交的， 扣罚金额，扣罚备注只展示不可修改

        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));

        loanDetail.setDataTransferDate(loanDataAudit.getDataTransferDate());
        loanDetail.setMissingRemark(loanDataAudit.getMissingRemark());
        if (DataAuditStatus.SUBMIT.status == loanDataAudit.getOperatorStatus()) {
            loanDetail.setMissingRemark(StringUtils.EMPTY);
        }
        loanDetail.setDeductionCardNo(loanDataAudit.getDeductionCardNo());
        //判断结算备注
        if (PaymentSettleAccountsStatus.INIT.status != loanStatus.getSettleAccountsStatus()) {
            loanDetail.setSalesmanFinedAmount(loanDataAudit.getSalesmanFinedAmount());
            loanDetail.setSalesmanRemark(loanDataAudit.getSettleRemark());
        }
        loanDetailMapper.updateById(loanDetail);
        //修改状态
        loanStatus.setDataAuditStatus(loanDataAudit.getOperatorStatus());
        loanStatus.setDataAuditTime(LocalDateTime.now());
        loanStatus.setDataAuditId(SecurityUtils.getLoginUserId());
        loanStatusMapper.updateById(loanStatus);

        LoanAlbum loanAlbumDelete = new LoanAlbum();
        loanAlbumDelete.setLoanId(loanId);
        loanAlbumDelete.setType(LoanAlbumType.CAR_INFO.type);
        QueryWrapper<LoanAlbum> queryWrapper = new QueryWrapper<>(loanAlbumDelete);
        loanAlbumMapper.delete(queryWrapper);

        List<LoanAlbumVo> carInfoAlbums = loanDataAudit.getCarInfoAlbums();
        if (null != carInfoAlbums) {
            carInfoAlbums.forEach(carInfoAlbum -> {
                LoanAlbum loanAlbum = new LoanAlbum();
                loanAlbum.setUrl(carInfoAlbum.getDbUrl());
                loanAlbum.setType(LoanAlbumType.CAR_INFO.type);
                loanAlbum.setLoanId(loanId);
                loanAlbumMapper.insert(loanAlbum);
            });
        }
        return AjaxResult.success();
    }
}
